package Final;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
 
import javax.swing.*;
 
public class DesktopFoo extends JPanel {
   private static final Dimension DESKTOP_SIZE = new Dimension(800, 600);
   private static final int COLS = 5;
   private static final int ROWS = 6;
   private static final int JIF_MAX = 4;
   private static final Dimension INTERNAL_FRAME_SIZE = new Dimension(300, 200);
   private JDesktopPane desktop = new JDesktopPane();
   private JPanel backgroundPanel = new JPanel();
    
   public DesktopFoo() {
      backgroundPanel.setSize(DESKTOP_SIZE);
      backgroundPanel.setLocation(0, 0);
      backgroundPanel.setBackground(Color.pink);
      int gap = 20;
      backgroundPanel.setLayout(new GridLayout(ROWS, COLS, gap, gap));
      backgroundPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
      for (int i = 0; i < ROWS; i++) {
         for (int j = 0; j < COLS; j++) {
            String text = "[" + j + ", " + i + "]";
            JButton btn = new JButton(text);
            backgroundPanel.add(btn);
         }
      }
       
      for (int i = 0; i < JIF_MAX; i++) {
         JInternalFrame internalFrame = new JInternalFrame("Internal Frame " + i);
         internalFrame.setPreferredSize(INTERNAL_FRAME_SIZE);
         internalFrame.pack();
         int offset = 60;
         internalFrame.setLocation(offset + offset * i, offset + offset * i);
         desktop.add(internalFrame);
         internalFrame.setVisible(true);         
      }
       
      desktop.setPreferredSize(DESKTOP_SIZE);
      desktop.add(backgroundPanel, JLayeredPane.DEFAULT_LAYER);
       
      setLayout(new BorderLayout());
      add(desktop, BorderLayout.CENTER);
   }
 
   private static void createAndShowUI() {
      JFrame frame = new JFrame("DesktopFoo");
      frame.getContentPane().add(new DesktopFoo());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
 
   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
